package com.temp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.temp.dto.SearchRoleDTO;
import com.temp.entity.Role;
import com.temp.vo.UserRolePermissionVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    @Select("SELECT " +
            "u.id, " +
            "r.name AS role_name, " +
            "r.description, " +
            "p.name AS perm_name " +
            "FROM " +
            "user u " +
            "JOIN " +
            "role r ON r.id = u.role_id " +
            "LEFT JOIN " +
            "role_permission rp ON rp.role_id = r.id " +
            "LEFT JOIN " +
            "permission p ON p.id = rp.permission_id " +
            "WHERE " +
            "r.status = 1 and p.status = 1 and u.id = #{userId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "description", column = "description"),
            @Result(property = "permName", column = "perm_name")
    })
    List<UserRolePermissionVO> findUserRolesAndPermissions(@Param("userId") Long userId);

    List<Role> findAllRole(SearchRoleDTO roleDTO);

    Integer countAllRole(SearchRoleDTO roleDTO);
}
